package com.hkts.flow.manager.api;

import com.hkts.flow.feign.FeignInterface;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.User;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author : bruce.liu
 * @title: : BaseResource
 * @projectName : flowable
 * @description: TODO
 * @date : 2019/11/2216:18
 */
public class BaseResource {
    @Autowired
    private FeignInterface feignInterface;
    @Autowired
    protected RuntimeService runtimeService;

    public User getLoginUser() {
        Map<String, Object> stringObjectMap = feignInterface.remoteUserInfo();
        //User user = SecurityUtils.getCurrentUserObject();
        User currentUserObject = new User() {
            @Override
            public String getId() {
                return stringObjectMap.get("id").toString();
            }

            @Override
            public void setId(String id) {

            }

            @Override
            public String getFirstName() {
                return stringObjectMap.get("nickName").toString();
            }

            @Override
            public void setFirstName(String firstName) {

            }

            @Override
            public void setLastName(String lastName) {

            }

            @Override
            public String getLastName() {
                return stringObjectMap.get("nickName").toString();
            }

            @Override
            public void setDisplayName(String displayName) {

            }

            @Override
            public String getDisplayName() {
                return stringObjectMap.get("nickName").toString();
            }

            @Override
            public void setEmail(String email) {

            }

            @Override
            public String getEmail() {
                return stringObjectMap.get("email").toString();
            }

            @Override
            public String getPassword() {
                return stringObjectMap.get("password").toString();
            }

            @Override
            public void setPassword(String string) {

            }

            @Override
            public String getTenantId() {
                return null;
            }

            @Override
            public void setTenantId(String tenantId) {

            }

            @Override
            public boolean isPictureSet() {
                return false;
            }
        };
        return currentUserObject;
    }

    /**
     * 判断是否挂起状态
     * @param processInstanceId 流程实例id
     * @return
     */
    public boolean isSuspended(String processInstanceId) {
        boolean flag = true;
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if (processInstance != null){
            flag = !processInstance.isSuspended();
        }
        return flag;
    }
}
